/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.library.util;

import java.io.OutputStream;
import java.io.PrintWriter;

import de.teamskill.pux.engine.ConfigurationPort;
import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.OutputPort;

/**
 * PrintNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class PrintNode extends AbstractNode {

	/**
	 * CONFIG_OUTPUTSTREAM
	 */
	public static final String CONFIG_OUTPUTSTREAM = "outputStream";

	/**
	 * channels
	 */
	private int channels;

	/**
	 * configOutputStream
	 */
	private ConfigurationPort configOutputStream;

	/**
	 * PrintNode Constructor
	 * 
	 */
	public PrintNode() {
		this("PRINT" + nr++);
	}

	/**
	 * PrintNode Constructor
	 * 
	 * @param name
	 */
	public PrintNode(String name) {
		this(name, 1, false);
	}

	/**
	 * PrintNode Constructor
	 * 
	 * @param name
	 * @param continuous
	 */
	public PrintNode(String name, int channels, boolean continuous) {
		super(name, continuous);
		this.channels = channels;
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	@Override
	public void compute() {
		StringBuffer output = new StringBuffer();
		for (int i = 0; i < channels; i++) {
			InputPort ip = getInputPort("IN" + (i + 1));
			OutputPort op = getOutputPort("OUT" + (i + 1));
			Object data = ip.fetchData();
			output.append(data);
			if (i < (channels - 1)) {
				output.append(", ");
			}

			// forward the data
			if (op.getRemotePort() != null) {
				op.pushData(data);
			}
		}

		// print out the content
		String m = output.toString();

		// log the data
		LOG_NODE.fine(m);

		OutputStream os = (OutputStream) configOutputStream.fetchData();
		PrintWriter pw = new PrintWriter(os);
		pw.println(getNodeName() + "> " + m);
		pw.flush();
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		for (int i = 0; i < channels; i++) {
			addInputPort(new InputPort("IN" + (i + 1), this, true));
			addOutputPort(new OutputPort("OUT" + (i + 1), this));
		}
		configOutputStream = addConfigurationPort(CONFIG_OUTPUTSTREAM, OutputStream.class, System.out);
	}

	/**
	 * JavaBean-style getter-method for channels
	 * 
	 * @return the channels
	 */
	public int getChannels() {
		return channels;
	}

	/**
	 * /** JavaBean-style setter-method for channels
	 * 
	 * @param channels
	 *            the channels to set
	 */
	public void setChannels(int channels) {
		this.channels = channels;
	}

	/**
	 * JavaBean-style getter-method for configOutputStream
	 * 
	 * @return the configOutputStream
	 */
	public ConfigurationPort getConfigOutputStream() {
		return configOutputStream;
	}

	/**
	 * /** JavaBean-style setter-method for configOutputStream
	 * 
	 * @param configOutputStream
	 *            the configOutputStream to set
	 */
	public void setConfigOutputStream(ConfigurationPort configOutputStream) {
		this.configOutputStream = configOutputStream;
	}

}
